package com.sangcomz.fishbun;

import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.app.Context;


public final class FishBun {
    private final Ability _activity;
    private final AbilitySlice _abilitySlice;
    public static final int FISHBUN_REQUEST_CODE = 27;
    public static final String INTENT_PATH = "intent_path";

    public final FishBun.FishBunContext getFishBunContext() {
        return new FishBun.FishBunContext();
    }

    public static FishBun with(Ability ability) {
        return new FishBun(ability, null);
    }

    public static FishBun with(AbilitySlice abilitySlice) {
        return new FishBun(null, abilitySlice);
    }

    public final FishBunCreator setImageAdapter(ImageAdapter imageAdapter) {
        Fishton fishton = Fishton.getInstance();
        fishton.refresh();
        fishton.imageAdapter = imageAdapter;
        return new FishBunCreator(this, fishton);
    }

    public FishBun(Ability activity, AbilitySlice abilitySlice) {
        this._activity = activity;
        this._abilitySlice = abilitySlice;
    }

    public final class FishBunContext {
        private Ability ability;
        private AbilitySlice fraction;

        public final Context getContext() {
            ability = _activity;
            Context context;
            if (ability != null) {
                context = ability;
            } else {
                fraction = _abilitySlice;
                context = fraction != null ? fraction.getContext() : null;
            }

            if (context != null) {
                return context;
            } else {
                try {
                    throw (Throwable) (new NullPointerException("Activity or Fragment Null"));
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                return null;
            }
        }

        public final void startActivityForResult(Intent intent, int requestCode) {
            if (ability != null) {
                ability.startAbilityForResult(intent, requestCode);
            } else {
                if (fraction == null) {
                    try {
                        throw (Throwable) (new NullPointerException("Activity or Fragment Null"));
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                    }
                }
                fraction.startAbilityForResult(intent, requestCode);
            }

        }

        public FishBunContext() {
            this.ability = FishBun.this._activity;
            this.fraction = FishBun.this._abilitySlice;
        }
    }
}
